//
//  MBMainViewController.swift
//  MicroBlog
//
//  Created by ChenJunWen on 2017/11/28.
//  Copyright © 2017年 CJW. All rights reserved.
//

import UIKit

class MBMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        setupChildController()
        
        setupComposeBtn()
    }
    
    // MARK: - 私有控件
    lazy var composeBtn: UIButton = UIButton.jw_imageButton("tabbar_compose_icon_add", backgroundImageName: "tabbar_compose_button")
    
    // 使用@objc 允许这个函数在运行时通过OC的消息机制被调用
    @objc func composeBtnClick() -> () {
        print("发微博")
    }
}

/* 设置底部tabbar */
extension MBMainViewController {
    
    /* 设置撰写按钮 */
    func setupComposeBtn() -> () {
        tabBar.addSubview(composeBtn)
        
        let count = CGFloat(childViewControllers.count)
        let itemWidth = tabBar.bounds.width / count - 1 // -1是为了撰写按钮点击的范围扩大

        composeBtn.frame = tabBar.bounds.insetBy(dx: 2 * itemWidth, dy: 0)
        composeBtn.addTarget(self, action: #selector(composeBtnClick), for: .touchUpInside)
    }
    
    /* 初始化控制器 */
    func setupChildController() {
        
        let dictArray = [
            ["className": "MBHomeViewController", "title": "首页", "imageName": "home"],
            ["className": "MBMessageViewController", "title": "消息", "imageName": "message_center"],
            ["className": "UIViewController"], // 预留撰写按钮的位置
            ["className": "MBDiscoverViewController", "title": "发现", "imageName": "discover"],
            ["className": "MBProfileViewController", "title": "我", "imageName": "profile"],
        ]
        
        var controllersM = [UIViewController]()
        for dict in dictArray {
            controllersM.append(controller(dict: dict as [String : AnyObject]))
        }
        // 设置
        viewControllers = controllersM;
    }
    
    /* 使用字典创建子控制器 */
    private func controller(dict: [String : AnyObject]) -> UIViewController {
        guard let className = dict["className"],
              let title = dict["title"] as? String,
              let imageName = dict["imageName"] as? String,
              // swift有命名空间,命名空间就是项目名
              let cls = NSClassFromString(Bundle.main.nameSpace + "." + (className as! String)) as? MBBaseViewController.Type
        else {
            return UIViewController()
        }
        
        // 创建视图控制器
        let vc = cls.init()
        vc.title = title
        vc.tabBarItem.image = UIImage(named: "tabbar_" + imageName)
        vc.tabBarItem.selectedImage = UIImage(named: "tabbar_" + imageName + "_selected")?.withRenderingMode(.alwaysOriginal)
        
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:UIColor.orange], for: .selected)
        vc.tabBarItem.setTitleTextAttributes([NSFontAttributeName: UIFont.systemFont(ofSize: 12)], for: .normal)
        
        let nav = MBNavigationController(rootViewController: vc)
        return nav
        
    }
    
}
